package com.hqyj.freemovie.front.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.freemovie.front.domain.Users;
import com.hqyj.freemovie.front.mapper.UsersMapper;
import com.hqyj.freemovie.front.service.UsersService;
import com.hqyj.freemovie.front.util.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Override

    @Transactional(rollbackFor = Exception.class)
    public boolean reg(Users u) throws Exception {
        try {
            //随机生成一个用户名
            u.setUsername(MyUtil.generateRandomUsername());
            //对用户的密码进行MD5+盐进行密码，加密后密码保存数据库中，这个很安全。

            u.setPassword(MyUtil.encodePasswordByMd5Salt(u.getPassword()));
            u.setGender("男");//性别默认是男

            u.setBirthday(DateUtil.today());
            usersMapper.insert(u);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            //这句话必须写，否则不会回滚....
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public Users login(String identify, String password) {
        try {
            QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
            if (MyUtil.isMobileNumber(identify)) {
                queryWrapper.eq("mobile", identify);

            } else {
                queryWrapper.eq("username", identify);
            }
            queryWrapper.eq("password", MyUtil.encodePasswordByMd5Salt(password));
            Users loginUser = usersMapper.selectOne(queryWrapper);
            return loginUser;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public Users queryUserByUid(Integer uid) {
        return usersMapper.selectById(uid);
    }

    @Override
    public boolean updateUser(Users u) throws Exception {
        try {
            usersMapper.updateById(u);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            //这句话必须写，否则不会回滚....
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public boolean removeUser(Integer uid) throws Exception {
        try {
            usersMapper.deleteById(uid);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            //这句话必须写，否则不会回滚....
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public boolean checkMobileIsReg(String mobile) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("mobile",mobile);
        List<Users> usersList = usersMapper.selectList(queryWrapper);
        return usersList != null && usersList.size() > 0;
    }
}
